//
//  SimWebView.h
//  Knowbox
//
//  Created by huxiulei on 14/11/20.
//  Copyright (c) 2014年 knowin. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "SimDefine.h"

extern NSString * const kSimWebListenAudioStopNoti;

SimDefineValue(kSimWebImageFailedIcon, NSString *);
SimDefineValue(kSimWebImageFailedText, NSString *);

SimDefineValue(kSimWebCSSRemoteURLStr, NSString *);
SimDefineValue(kSimWebCSSLocalFilePath, NSString *);
SimDefineValue(kSimWebJSRemoteURLStr, NSString *);
SimDefineValue(kSimWebJSLocalFilePath, NSString *);


typedef NS_OPTIONS(NSUInteger, WebViewAutoFitType) {
    WebViewAutoFitNone          = 0,
    WebViewAutoFitHeight        = 1 << 0,
    WebViewAutoFitWidth         = 1 << 1,
};


typedef NS_ENUM(NSInteger, WebviewFinishStatus){
    WFS_None,
    WFS_Loaded,
    WFS_LoadError,
};

//State tracking for load progress of current page
typedef struct {
    NSInteger   loadingCount;       //Number of requests concurrently being handled
    NSInteger   maxLoadCount;       //Maximum number of load requests that was reached
    BOOL        interactive;        //Load progress has reached the point where users may interact with the content
    CGFloat     loadingProgress;    //Between 0.0 and 1.0, the load progress of the current page
} LoadingProgressState;

@class SimWebView;
typedef void(^WebViewCompleteLoad)(SimWebView *webView, WebviewFinishStatus finishStatus);

@interface SimWebView : UIWebView<UIWebViewDelegate>

//question html string
@property (nonatomic, readonly) NSString *htmlContent;
@property (nonatomic, assign) WebViewAutoFitType  autoFitType;
@property (nonatomic, assign) CGFloat fontSize;
@property (nonatomic, assign) CGFloat lineHeight;
@property (nonatomic, strong) UIColor *textColor;
@property (nonatomic, assign) NSInteger orderNum;
@property (nonatomic, assign) BOOL bShowDetailPic;

- (void)loadHTMLString:(NSString *)string complete:(WebViewCompleteLoad)complete;
- (BOOL)isContentFinishLoad;
- (void)htmlFailLoadToRetry;
- (void)stopAllAudio;
- (void)finishLoadProgress:(NSError *)error;

//URL request
@property (nonatomic, readonly) NSURL *webviewURL;
@property (nonatomic, assign) NSURLRequestCachePolicy cachePolicy;
@property (nonatomic, assign) NSTimeInterval requestInterval;
- (void)loadURLStr:(NSString *)URLStr complete:(WebViewCompleteLoad)complete;
- (void)loadURL:(NSURL *)URL complete:(WebViewCompleteLoad)complete;
- (BOOL)handlerRequest:(NSString *)requestStr navigationType:(UIWebViewNavigationType)navigationType;

//delegate
@property (nonatomic, strong) BOOL (^shouldStartLoadRequestHandler)(NSURLRequest *request, UIWebViewNavigationType navigationType);
@property (nonatomic, strong) void (^startLoad)();
@property (nonatomic, strong) void (^resetLoad)();
@property (nonatomic, strong) void (^reLoad)();
@property (nonatomic, strong) WebViewCompleteLoad completeLoad;

//loading bar
@property (nonatomic, readonly) UIView *loadingBarView;
@property (nonatomic, readonly) LoadingProgressState loadingProgressState;
@property (nonatomic, strong) UIColor *loadingBarTintColor;
@property (nonatomic, assign) BOOL showLoadingBar;

//load status
@property (nonatomic, readonly) WebviewFinishStatus finishStatus;
@property (nonatomic, readonly) CGSize scrollSize;

- (void)reloadWebView;
- (UIColor *)webViewPageBackgroundColor;
@property (nonatomic, assign) BOOL disableContextualPopupMenu;



@end
